package com.powroznik.tracker.liferay.portlet;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.RenderRequest;
import javax.servlet.http.HttpServletRequest;

import com.powroznik.tracker.engine.services.Account;
import com.powroznik.tracker.engine.services.Device;
import com.powroznik.tracker.engine.services.Location;
import com.powroznik.tracker.engine.services.PortalService;
import com.powroznik.tracker.engine.services.PortalServiceProxy;
import com.powroznik.tracker.engine.services.User;

public class Client {
	public static Client getInstance(HttpServletRequest req) {
		Client instance = (Client) req.getSession().getAttribute(
				Client.class.getName() + ".instance");
		if (instance == null) {
			instance = new Client();
			req.getSession().setAttribute(Client.class.getName() + ".instance",
					instance);
		}
		return instance;
	}

	public static Client getInstance(RenderRequest req) {
		Client instance = (Client) req.getPortletSession().getAttribute(
				Client.class.getName() + ".instance");
		if (instance == null) {
			instance = new Client();
			instance.setUserKey(req.getRemoteUser());
			req.getPortletSession().setAttribute(
					Client.class.getName() + ".instance", instance);
		}
		return instance;
	}

	private PortalService portalService = new PortalServiceProxy();

	private String userKey;

	public void connectDevice(String code1, String description) {
		try {
			Account account = getAccount();
			Device device = portalService.getDeviceByCode1(code1);
			portalService.connectDevice(account, device, description);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Account getAccount() {
		User user = getUser();
		if (user == null) {
			return null;
		}
		return getAccountByUser(user);
	}

	private Account getAccountByUser(User user) {
		try {
			return portalService.getAccountByUser(user);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<Device> getAllDevices() {
		try {
			List<Device> list = new ArrayList<Device>();
			Device[] array = portalService.getAllDevices();
			if (array != null) {
				for (Device device : array) {
					list.add(device);
				}
			}
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<Device> getConnectedDevices() {
		try {
			List<Device> list = new ArrayList<Device>();
			Device[] array = portalService.getDevicesByAccount(getAccount());
			if (array != null) {
				for (Device device : array) {
					list.add(device);
				}
			}
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Location getLastKnownLocation(Device device) {
		try {
			return portalService.getLastKnownLocation(device);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private User getUser() {
		return getUserByKey(userKey);
	}

	private User getUserByKey(String key) {
		try {
			return portalService.getUserByKey(key);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void registerNewAccount(Account account) {
		try {
			portalService.registerNewAccount(getUser(), account);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setUserKey(String userKey) {
		this.userKey = userKey;
	}

	public void updateAccount(Account account) {
		try {
			portalService.updateAccount(getUser(), account);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
